﻿using System;
using System.Collections.Generic;

using System.Text;
using HZ.Data;
using System.Data;

namespace HZ.DrugTrade.DAL
{
    public class DAL_AreaDistribution
    {
        /// <summary>
        /// 获取地区分配送信息
        /// </summary>
        /// <param name="where"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public static IList<HZ.DrugTrade.Model.View.AreaDistribution> GetAreaDistribution(HZ.DrugTrade.Model.Query.AreaDistribution where, int currentPage, int pageSize, out int totalRecord)
        {
            string wherestr = HZ.Data.DAL_Helper.GetWhereCondition(where,true);
            string sql = string.Format("select * from (SELECT a.*,case a.Unioncitystate when 1 then '审核通过' when 2 then '审核不通过' else '未审核' end as Unioncitystateda FROM View_AreaDistribution a )WHERE 1=1 {0}", wherestr);
            HZ.Data.PageDescribe pdes = new HZ.Data.PageDescribe(sql, pageSize) { CurrentPage = currentPage };
            DataTable dt = pdes.GetSpecailPage();
            totalRecord = pdes.TotalCount;
            return HZ.Data.DAL_Helper.CommonFillList<HZ.DrugTrade.Model.View.AreaDistribution>(dt);

        }
        /// <summary>
        /// 主配送获取可管理配送产品
        /// </summary>
        /// <param name="where"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        /// 获取企业可管理产品使用 View_ProcureCatalog by lig 20091115
        //public static IList<HZ.DrugTrade.Model.View.DistributionManage> GetAreaDistributionManage(HZ.DrugTrade.Model.Query.DistributionManage where, int currentPage, int pageSize, out int totalRecord)
        //{
        //    string wherestr = HZ.Data.DAL_Helper.GetWhereCondition(where,true);
        //    string sql = string.Format("SELECT * FROM View_DistributionManage  WHERE 1=1 {0}", wherestr);
        //    HZ.Data.PageDescribe pdes = new HZ.Data.PageDescribe(sql, pageSize) { CurrentPage = currentPage };
        //    DataTable dt = pdes.GetSpecailPage();
        //    totalRecord = pdes.TotalCount;
        //    return HZ.Data.DAL_Helper.CommonFillList<HZ.DrugTrade.Model.View.DistributionManage>(dt);
        //}

        public static IList<HZ.DrugTrade.Model.Company> GetDistributionRelationByHospital(string ProcureCatalogID, string areaid)
        {
            //todo:GetDistributionRelationByHospital未完成！
            return null;
        }

        public static IList<Model.View.ProcureCatalog> GetTransformProcureCatalogTo(string companyID, HZ.DrugTrade.Model.Query.ProcureCatalog where, int currentpage, int pagesize, out int totalrecord)
        {
            string sql = string.Format(@"SELECT v.ProductName,
	   v.MedicineModelName,
	   v.Outlookc,
	   v.CompanyName_SC,
	   v.Unit,
	   v.SortName,
	   v.GoodsName,
	   v.Factor,
	   v.Pack,
	   v.ProcureCatalogID,
	   v.ProcurePrice,
	   v.IsUsing,
	   v.IsFlowing,
	   v.SortSource,
	   v.ProjectName,
	   v.PinyinCode,
	   v.WBCode FROM View_ProcureCatalog  AS v WHERE v.ProcureCatalogID IN (
SELECT ProcureCatalogID FROM TB_TransformDistributionLog WHERE LastCompanyID = '{0}'){1}", companyID, HZ.Data.DAL_Helper.GetWhereCondition(where,true));
            PageDescribe des = new PageDescribe(sql, currentpage);
            des.CurrentPage = currentpage;
            totalrecord = des.TotalCount;
            return HZ.Data.DAL_Helper.CommonFillList<Model.View.ProcureCatalog>(des.GetSpecailPage());

        }

        public static IList<HZ.DrugTrade.Model.View.ProcureCatalog> GetTransformProcureCatalogFrom(string companyID, HZ.DrugTrade.Model.Query.ProcureCatalog where, int currentpage, int pagesize, out int totalrecord)
        {
            string sql = string.Format(@"SELECT v.ProductName,
	   v.MedicineModelName,
	   v.Outlookc,
	   v.CompanyName_SC,
	   v.Unit,
	   v.SortName,
	   v.GoodsName,
	   v.Factor,
	   v.Pack,
	   v.ProcureCatalogID,
	   v.ProcurePrice,
	   v.IsUsing,
	   v.IsFlowing,
	   v.SortSource,
	   v.ProjectName,
	   v.PinyinCode,
	   v.WBCode FROM View_ProcureCatalog  AS v WHERE v.ProcureCatalogID IN (
SELECT ProcureCatalogID FROM TB_TransformDistributionLog WHERE NowCompanyID = '{0}'){1}", companyID, HZ.Data.DAL_Helper.GetWhereCondition(where,true));
            PageDescribe des = new PageDescribe(sql, currentpage);
            des.CurrentPage = currentpage;
            totalrecord = des.TotalCount;
            return HZ.Data.DAL_Helper.CommonFillList<Model.View.ProcureCatalog>(des.GetSpecailPage());

        }

        public static IList<HZ.DrugTrade.Model.View.TransformDistributionLog> GetDistributionLogDown(string companyID, string areaID, string ProcureCatalogID)
        {
            string sql = string.Format(@"SELECT * FROM View_TransformDistributionLog
WHERE AreaID = '{0}' AND ProcureCatalogID = '{1}' ORDER BY TransformDistributeTime ASC", areaID, ProcureCatalogID);
            DataTable dt = HZ.Data.DbHelperFactory.Create().ExecuteTable(sql);
            IList<HZ.DrugTrade.Model.View.TransformDistributionLog> listorigin = HZ.Data.DAL_Helper.CommonFillList<HZ.DrugTrade.Model.View.TransformDistributionLog>(dt);

            IList<HZ.DrugTrade.Model.View.TransformDistributionLog> result = new List<HZ.DrugTrade.Model.View.TransformDistributionLog>();
            string currentcompanyID = companyID;
            //遍历
            foreach (var item in listorigin)
            {
                if (item.LastCompanyID == currentcompanyID)
                {
                    result.Add(item);
                    currentcompanyID = item.NowCompanyID;
                }
            }
            return result;

        }

        public static IList<HZ.DrugTrade.Model.View.TransformDistributionLog> GetDistributionLogUp(string companyID, string areaID, string ProcureCatalogID)
        {
            string sql = string.Format(@"SELECT * FROM View_TransformDistributionLog
WHERE AreaID = '{0}' AND ProcureCatalogID = '{1}' ORDER BY TransformDistributeTime DESC", areaID, ProcureCatalogID);
            DataTable dt = HZ.Data.DbHelperFactory.Create().ExecuteTable(sql);
            IList<HZ.DrugTrade.Model.View.TransformDistributionLog> listorigin = HZ.Data.DAL_Helper.CommonFillList<HZ.DrugTrade.Model.View.TransformDistributionLog>(dt);

            IList<HZ.DrugTrade.Model.View.TransformDistributionLog> result = new List<HZ.DrugTrade.Model.View.TransformDistributionLog>();
            string currentcompanyID = companyID;
            //遍历
            foreach (var item in listorigin)
            {
                if (item.NowCompanyID == currentcompanyID)
                {
                    result.Add(item);
                    currentcompanyID = item.LastCompanyID;
                }
            }
            return result;

        }

        public static int DeleteAreaDistribution_Batch(IList<HZ.DrugTrade.Model.AreaDistribution> DeleteList)
        {

            var h = HZ.Data.DbHelperFactory.Create();
            try
            {
                h.BeginTrans();
                foreach (var item in DeleteList)
                {
                  //  h.ExecuteNonQuery(HZ.Data.DAL_Helper.GetDeleteSql(item));
                    HZ.Data.DAL_Helper.OperateTableSQL<HZ.DrugTrade.Model.AreaDistribution>(item,HZ.Data.TableOperator.Delete,h);
                }

                h.CommitTrans();

            }
            catch (Exception ee)
            {
                HZ.Common.log4netManager.LogDebug("DAL_InvoicePay", ee.Message, ee);
                h.RollTrans();
                throw;
            }
            return 1;
        }
    }
}
